<template>
  <div class="comment-item" :class="{ 'comment-item--mine': isMine }">
    <div class="comment-item-title">
      <span class="comment-item-title-header">{{ props.commentInfo.username }}</span>
      <span class="comment-item-title-time">{{ formatTime }}</span>
    </div>
    <div class="comment-item-bg">
      <span class="comment-item-content">
        <span v-for="(item, index) in commentInfo.comment.split('\n')" :key="index">{{
          item
        }}</span>
      </span>
    </div>
  </div>
</template>

<script setup>
  import { useChatStore } from '@/store/chat'
  const chatStore = useChatStore()

  const props = defineProps({
    commentInfo: Object,
  })

  const formatTime = computed(() => dayjs(props.commentInfo.time).format('MM-DD HH:mm:ss'))

  const isMine = computed(() => props.commentInfo.username === chatStore.currentUserName)
</script>

<style lang="less" scoped>
  .comment-item {
    box-sizing: border-box;
    display: flex;
    flex-flow: column nowrap;
    align-items: flex-start;
    width: 100%;
    height: auto;
    margin: 20px 0;
    padding: 0 0 0 14px;
    .comment-item-title {
      display: flex;
      align-items: center;
      line-height: 26px;
      margin-bottom: 4px;
      .comment-item-title-header {
        transition: 0.26s;
        margin-right: 8px;
        font-size: 13px;
        color: var(--van-text-color-2);
        font-weight: bold;
      }
      .comment-item-title-time {
        transition: 0.26s;
        color: var(--van-text-color-3);
        font-size: 12px;
      }
    }

    .comment-item-bg {
      transition: 0.26s;
      position: relative;
      display: inline-block;
      max-width: 80%;
      overflow: hidden;
      word-break: break-all;
      background: var(--me-other-gradient-bg);
      color: var(--van-text-color);
      padding: 12px;
      padding-top: 10px;
      border-radius: 8px 8px 8px 0;
      .comment-item-content {
        font-size: 13px;
        span {
          display: block;
        }
      }
      &::after {
        content: '';
        position: absolute;
        left: -8px;
        bottom: 0;
        width: 0;
        height: 0;
        border-left: 6px solid transparent;
        border-top: 6px solid transparent;
        border-right: 6px solid var(--me-other-bg);
        border-bottom: 4px solid var(--me-other-bg);
      }
    }
    &--mine {
      display: flex;
      align-items: flex-end;
      padding: 0 14px 0 0;
      .comment-item-bg {
        background: var(--me-gradient-bg);
        color: var(--me-text);
        border-radius: 8px 8px 0 8px;
        &::after {
          content: '';
          position: absolute;
          left: initial;
          right: -8px;
          bottom: 0;
          width: 0;
          height: 0;
          border-top: 6px solid transparent;
          border-right: 6px solid transparent;
          border-left: 6px solid var(--me-bg);
          border-bottom: 4px solid var(--me-bg);
        }
        .comment-item-title {
          border-bottom: 1px solid #333;
        }
      }
    }
  }
</style>
